package com.citcc.business.service;

import com.citcc.business.bean.*;
import com.citcc.business.mapper.*;
import com.citcc.business.util.CollectionUtil;

import com.citcc.common.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: qinbo
 * Date: 2019/4/6
 * Time: 13:57
 * Description:
 */
@Service
@Transactional
public class ScanCodeService {

    @Autowired
    private CdzGunMapper cdzGunMapper;

    @Autowired
    private CdzStakeMapper cdzStakeMapper;
    @Autowired
    private CdzIndentMapper cdzIndentMapper;

    @Autowired
    private UserOrderEntityMapper userOrderEntityMapper;

    @Autowired
    private CdzChargingEntityMapper cdzChargingEntityMapper;
    /**
     * 判断充电桩是否空闲
     */
    public CdzGun checkCdzGun(String cdq, String cdz) {
        if (StringUtils.isEmpty(cdq)){
            cdq="01";
        }
        List<CdzGun> list = cdzGunMapper.selectByCdqAndCdz(cdq, cdz);
        if (CollectionUtil.isNotEmpty(list)) {
            CdzGun cdzGun = list.get(0);
            return cdzGun;
        }
        return null;
    }

    /**
     * 判断充电桩是否空闲
     */
    public CdzGun getCdzGunByNum(String cdq) {
        CdzGun cdzGun = cdzGunMapper.selectByCdq(cdq);
        return cdzGun;
    }

    /**
     * 根据code获取充电装
     */
    public CdzStake getCdzStakeBycode(String code) {
        CdzStake cdzStake = cdzStakeMapper.getByStakeCode(code);
        return cdzStake;
    }

    /**
     * 查询订单
     *
     * @param id
     * @param type
     * @return
     */
    public List<UserOrderEntity> getOrderList(String id, String type,RowBounds rowBounds) {
        UserOrderEntityExample userOrderEntityExample=new UserOrderEntityExample();
        userOrderEntityExample.setOrderByClause("CREATE_TIME desc");
        switch (type){
            case "0":
                List<String> list = Arrays.asList("0", "2","3","4");
                userOrderEntityExample.createCriteria().andDelFlagEqualTo("0").andUserIdEqualTo(id).andChargeStateIn(list);
                break;
            case "1":
                userOrderEntityExample.createCriteria().andUserIdEqualTo(id).andDelFlagEqualTo("0").andChargeStateEqualTo("0");
                break;
            case "2":
                userOrderEntityExample.createCriteria().andUserIdEqualTo(id).andDelFlagEqualTo("0").andChargeStateEqualTo("2");
                break;
            case "3":
                userOrderEntityExample.createCriteria().andUserIdEqualTo(id).andDelFlagEqualTo("0").andChargeStateEqualTo("3");
                break;
                default:
                    userOrderEntityExample.createCriteria().andUserIdEqualTo(id).andDelFlagEqualTo("0").andChargeStateIn(Arrays.asList("0", "2","3"));
                    break;
        }
        List<UserOrderEntity> userOrderEntities = userOrderEntityMapper.selectByExampleWithPage(userOrderEntityExample,rowBounds);
        return userOrderEntities;
    }

    /**
     * 判断充电桩是否空闲
     */
    public boolean saveOrder(UserOrderEntity userOrderEntity) {
        userOrderEntityMapper.insert(userOrderEntity);
        return true;
    }

    /**
     * 查询订单
     * @param orderNum
     * @return
     */
    public UserOrderEntity getCdOrder(String orderNum) {
        UserOrderEntity userOrderEntity = userOrderEntityMapper.selectByPrimaryKey(orderNum);
        return userOrderEntity;
    }

    /**
     *修改订单状态
     * @param userOrderEntity
     * @return
     */
    public boolean updatePayState(UserOrderEntity userOrderEntity) {
        //同时修改枪号
        int i = userOrderEntityMapper.updateByPrimaryKeySelective(userOrderEntity);
        return i>0;
    }

    /**
     *修改订单状态
     * @param userOrderEntity
     * @return
     */
    public boolean updatePayStateAndGun(UserOrderEntity userOrderEntity) {
        //同时修改枪号
        CdzGun cdzGun=new CdzGun();
        cdzGun.setStakeCode(userOrderEntity.getStakeCode());
        cdzGun.setGunNum(userOrderEntity.getGunCode());
        cdzGun.setStatus("1");
        cdzGunMapper.updateStatus(cdzGun);

        int i = userOrderEntityMapper.updateByPrimaryKeySelective(userOrderEntity);
        return i>0;
    }

    /**
     *修改订单状态(异常)
     * @param userOrderEntity
     * @return
     */
    public boolean updatePayStateAndGunYC(UserOrderEntity userOrderEntity) {
        //同时修改枪号
        CdzGun cdzGun=new CdzGun();
        cdzGun.setStakeCode(userOrderEntity.getStakeCode());
        cdzGun.setGunNum(userOrderEntity.getGunCode());
        cdzGun.setStatus("2");
        cdzGunMapper.updateStatus(cdzGun);

        int i = userOrderEntityMapper.updateByPrimaryKeySelective(userOrderEntity);
        return i>0;
    }
    public UserOrderEntity selectOrderByOrderNum(String transaction_id) {
        UserOrderEntityExample userOrderEntityExample=new UserOrderEntityExample();
        userOrderEntityExample.setOrderByClause(" UPDATE_TIME desc");
        userOrderEntityExample.createCriteria().andOutTradeNoEqualTo(transaction_id).andDelFlagEqualTo("0");
        List<UserOrderEntity> userOrderEntities = userOrderEntityMapper.selectByExample(userOrderEntityExample);
        if (CollectionUtil.isNotEmpty(userOrderEntities)){
            return userOrderEntities.get(0);
        }
        return null;
    }

    /**
     *修改订单状态
     * @param userOrderEntity
     * @return
     */
    public boolean updateUserOrderEntity(UserOrderEntity userOrderEntity,UserOrderEntityExample example) {
        int i = userOrderEntityMapper.updateByExampleSelective(userOrderEntity, example);
        return i>0;
    }

    //查询订单信息
    public UserOrderEntity getUserOrderEntity(UserOrderEntityExample example) {
        List<UserOrderEntity> list = userOrderEntityMapper.selectByExample(example);
        if(CollectionUtils.isNotEmpty(list)){
        	return list.get(0);
        }
        return null;
    }

    /**
     *修改订单状态
     * @param userOrderEntity
     * @return
     */
    public boolean updateUserOrderEntityById(UserOrderEntity userOrderEntity) {
        int i = userOrderEntityMapper.updateByPrimaryKeySelective(userOrderEntity);
        return i>0;
    }

    public CdzChargingEntity getDL(String orderNum) {
        CdzChargingEntity cdzChargingEntity = cdzChargingEntityMapper.selectByPrimaryKey(orderNum);
        return cdzChargingEntity;
    }
}